package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Version: V1.0
 */
@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService{
    @Autowired
    private CheckGroupMapper checkGroupMapper;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        Page<CheckGroup> all = checkGroupMapper.selectByCondition(queryString);
        List<CheckGroup> result = all.getResult();
        long total = all.getTotal();
        return new PageResult(total, result);
    }

    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        checkGroupMapper.add(checkGroup);
        Integer checkGroupId = checkGroup.getId();
        if(checkitemIds!=null && checkitemIds.length>0){
            for (Integer checkItemId : checkitemIds) {
                Map map=new HashMap();
                map.put("checkGroupId",checkGroupId);
                map.put("checkItemId",checkItemId);
                checkGroupMapper.setCheckItemAndCheckGroup(map);
            }
        }
    }

    @Override
    public void deleteById(Integer id) {
       long count = checkGroupMapper.findCountById(id);
       if(count>0){
           throw new RuntimeException("该检查组已被引用,删除失败");
       }
       checkGroupMapper.deleteAssociation(id);
       checkGroupMapper.deleteById(id);
    }

    @Override
    public CheckGroup findById(Integer id) {
        return checkGroupMapper.findById(id);
    }

    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        return checkGroupMapper.findCheckItemIdsByCheckGroupId(id);
    }

    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {
        checkGroupMapper.edit(checkGroup);
        Integer checkGroupId = checkGroup.getId();
        checkGroupMapper.deleteAssociation(checkGroupId);
        if(checkitemIds!=null && checkitemIds.length>0){
            for (Integer checkItemId : checkitemIds) {
                Map map=new HashMap();
                map.put("checkGroupId",checkGroupId);
                map.put("checkItemId",checkItemId);
                checkGroupMapper.setCheckItemAndCheckGroup(map);
            }
        }
    }

    @Override
    public List<CheckGroup> findAll() {
        return checkGroupMapper.findAll();
    }


}
















